const assert = require('assert');

// 某面试题
function _question1() {
  class Base {
    static instance() {
      return new this
    }

    name1() {
      return this.constructor.toString().replace(/^class (\b[a-zA-Z+]\b).*$/, ($, $1) => $1)
    }

    static name2() {
      return this.toString().replace(/^class (\b[a-zA-Z+]\b).*$/, ($, $1) => $1)
    }
  }

  class A extends Base {
  }

  class B extends Base {
  }

  assert.strictEqual(A.instance().name1(), 'A');
  assert.strictEqual(B.instance().name1(), 'B');
  assert.strictEqual(A.name2(), 'A');
  assert.strictEqual(B.name2(), 'B');
  console.log('验证通过');
}

// 寻找质数/判断质数
function _prime() {
  const prime = (function () {
    const arr = [2];

    function add(num) {
      if (!arr.some(item => num % item === 0))
        arr.push(num)
    }

    return {
      get(index) {
        let max = arr[arr.length - 1];
        while (!arr[index - 1])
          add(++max);
        return arr.slice(0, index);
      },
      is(number) {
        let max = arr[arr.length - 1];
        while (max < number)
          add(++max);
        return arr.some(item => item === number);
      },
      now() {
        return Object.assign([], arr)
      }
    }
  })();
  console.log('now() ' + prime.now());
  console.log('get(5) ' + prime.get(5));
  console.log('now() ' + prime.now());
  console.log('23 ? ' + prime.is(23));
  console.log('now() ' + prime.now());
  console.log('get(3) ' + prime.get(3));
  console.log('50 ? ' + prime.is(50));
  console.log('now() ' + prime.now());
}

//自己实现new
function _new() {
  function _new(Fn, ...params) {
    let ret = Object.create(Fn.prototype);
    Fn.call(ret, ...params);
    return ret
  }

  function Dog(name) {
    this.name = name;
  }

  let dog = _new(Dog, 'xiaoming');
  console.log(dog);
  console.log(dog instanceof Dog);
  console.log(dog.constructor);
}

//关于作用域
function _setTimeout() {
  for (var i = 0; i < 5; i++) {
    setTimeout((() => () => console.log(i))(), 300)
  }
}

//快速排序
function _quickSort() {
  let arr = [1, 5, 6, 7, 3, 4, 9, 2, 8, 24, 98, 26, 34, 78, 26, 49, 45, 28, 78, 19];

  function quickSort(arr) {
    if (arr.length <= 1)
      return arr;
    let middleIndex = Math.floor(arr.length / 2);
    let middleNumber = arr[middleIndex];
    let leftArr = [], rightArr = [];
    for (let i = 0; i < arr.length; i++) {
      if (i === middleIndex)
        continue;
      arr[i] < middleNumber ? leftArr.push(arr[i]) : rightArr.push(arr[i]);
    }
    return [...quickSort(leftArr), middleNumber, ...quickSort(rightArr)]
  }

  console.log(quickSort(arr));
}

//旋转数组
function _rotateArr() {
  let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  for (var i = 0; i < 3; i++)
    arr.unshift(arr.pop());
  console.log(arr);
}

//函数柯理化=>任意累加器
function _add() {
  function add(...a) {
    let res = 0;
    a.forEach(item => res += item);
    let fn = function (...b) {
      b.forEach(item => res += item);
      return fn
    };
    fn.toString = function () {
      return res
    };
    return fn
  }

  console.log(0 + add(1));
  console.log(add(1)(2) + '');
  console.log(add(1)(2)(3) - 0);
  console.log('------');
  console.log(0 + add(1, 2)(3));
  console.log(add(1)(2, 3) + '');
  console.log(add(1, 2, 3) - 0);
  console.log('------');
  console.log(0 + add(1, 2, 3, 4));
  console.log(add(1)(2)(3)(4) + '');
  console.log(add(1, 2)(3, 4) - 0);
}

// 击鼓传花：一群人做成一个圈，挨着往后数，数到n的人出列，完事从下一个继续数，问最后剩下谁
function _passGame() {
  function passGame(list, num) {
    let start = {value: list[0]}, point = start;
    for (let i = 1; i < list.length; i++) {
      point.next = {value: list[i]};
      point = point.next;
    }
    point.next = start;
    for (let a = 1; point !== point.next; a++, point = point.next) {
      if (a % num === 0) {
        point.next = point.next.next;
        a++;
      }
    }
    return point.value;
  }

  console.log(passGame([1, 2, 3, 4, 5, 6, 7, 8], 4));
}

module.exports = _passGame;
